#!/usr/bin/env python
# -*- coding: utf-8 -*- #   
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
import datetime
from google.appengine.api import users
import Cookie,urllib
from django.utils import simplejson
from google.appengine.api import urlfetch

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp.util import run_wsgi_app
import time
from google.appengine.ext import db
import SiteType
class CerIT(webapp.RequestHandler):
	def get(self):
		mem=db.get(self.request.get("key"))
		mem.waitforcer=False
		user=users.get_current_user()
		if user:
			if SiteType.isadmin(user):
				mem.put()
		self.redirect(self.request.headers['referer'])
	
class CerHandin(webapp.RequestHandler):
	def get(self):
		user=SiteType.Header(self,u"校友认证")
			
		if SiteType.Admined(self):
			if user:
				if SiteType.isadmin(user):
					mems=SiteType.Member.all()
					self.response.out.write(u"""<div align="center">认证<br>""")
					
					for mem in mems:
						if mem.waitforcer==False:
							continue
							
						outstr="<a href=/cerit?key=%s>"%mem.key()
						outstr=outstr+mem.name_chs
						outstr=outstr+(u"""%s"""%mem.year_of_grad)
						if mem.class_in_school:
							pass
						else:
							mem.class_in_school=""
							mem.put()
						outstr=outstr+mem.class_in_school+"</a><br>"
						if mem.waitforcer==True:
							self.response.out.write(outstr)	
							
						
					self.response.out.write(u"""</div""")
					SiteType.Ender(self)
					return
			self.response.out.write(u"""对不起，您没有进行此操作的权限""")
			SiteType.Ender(self)
			return
		self.response.out.write(u"""<head>
<script type="text/javascript">
function validate_form(thisform)
{
with (thisform)
  {
 var str=name.value;
  if (str=="")
  { 
  alert("对不起，姓名不能为空");
  return false;
  }
 
  return true;
  }
}
</script>
</head>""")
		if user:
			cered=SiteType.iscer(user)
			if cered==2:
				self.response.out.write(u"""您已经被认证为校友 欢迎你""")
			elif cered==1:
				self.response.out.write(u"请等待管理员认证")
			else:	
				self.response.out.write(u"""<div align="center"><div class="ss-form-container"> <div class="ss-form-heading"><h1 class="ss-form-title">&#20250;&#21592;&#35748;&#35777;</h1> 
<p></p> 
<div class="ss-form-desc">请认真填写此表。我们会尽全力保证您的隐私不受侵犯。<br />所有信息限于在附中人内部可阅。详情参见 《隐私管理方案》</div> 
<p></p> 
 
<hr class="ss-email-break" style="display:none;"> 
<div class="ss-required-asterisk">*必填</div></div> 
<div class="ss-form"><form action="/cercheck" method="POST" id="ss-form"> 
 
<br> 
<div class="errorbox-good"> 
<div class="ss-item  ss-section-header"><div class="ss-form-entry"><h2 class="ss-section-title">&#22522;&#26412;&#20449;&#24687;</h2> 
<div class="ss-section-description"></div> 
</div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_0">&#22995;&#21517;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_0"></label> 
<input type="text" name="entry.0.single" value="" class="ss-q-short" id="entry_0"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_1">&#33521;&#25991;&#21517;
</label> 
<label class="ss-q-help" for="entry_1"></label> 
<input type="text" name="entry.1.single" value="" class="ss-q-short" id="entry_1"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-radio"><div class="ss-form-entry"><label class="ss-q-title" for="entry_14">&#24615;&#21035;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_14"></label> 
<ul class="ss-choices"><li class="ss-choice-item"><input type="radio" name="entry.14.group" value="&#30007;" class="ss-q-radio" id="group_14_1"> 
<label class="ss-choice-label" for="group_14_1">&#30007;</label></li> <li class="ss-choice-item"><input type="radio" name="entry.14.group" value="&#22899;" class="ss-q-radio" id="group_14_2"> 
<label class="ss-choice-label" for="group_14_2">&#22899;</label></li> 
</ul></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_2">&#29983;&#26085;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_2">YYYY-MM-DD &#22914; 1989-06-04</label> 
<input type="text" name="entry.2.single" value="" class="ss-q-short" id="entry_2"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-section-header"><div class="ss-form-entry"><h2 class="ss-section-title">&#23398;&#26657;&#20449;&#24687;</h2> 
<div class="ss-section-description"></div> 
</div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-select"><div class="ss-form-entry"><label class="ss-q-title" for="entry_6">&#27605;&#19994;&#26102;&#38388;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_6"></label> 
<select name="entry.6.single" id="entry_6">""")
				td=datetime.date.today()
				for i in range(1971,td.year+1):
					self.response.out.write(u"""<option value="%s">"""%i+"""%s</option>"""%i)
				self.response.out.write(u"""</select></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-select"><div class="ss-form-entry"><label class="ss-q-title" for="entry_9">&#39640;&#20013;&#27605;&#19994;&#29677;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_9"></label> 
<select name="entry.9.single" id="entry_9">""")
				for i in range(1,11):
					self.response.out.write(u"""<option value="理科%d班">"""%i+u"""理科%d班</option>"""%i)
				for i in range(1,3):
					self.response.out.write(u"""<option value="文科%d班">"""%i+u"""文科%d班</option>"""%i)
				self.response.out.write(u"""</select></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_17">&#22823;&#23398;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_17"></label> 
<input type="text" name="entry.17.single" value="" class="ss-q-short" id="entry_17"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_15">&#19987;&#19994;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_15"></label> 
<input type="text" name="entry.15.single" value="" class="ss-q-short" id="entry_15"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-section-header"><div class="ss-form-entry"><h2 class="ss-section-title">&#31616;&#21382;</h2> 
<div class="ss-section-description"></div> 
</div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_20">&#26366;&#32463;&#23621;&#20303;&#22320;
</label> 
<label class="ss-q-help" for="entry_20"></label> 
<input type="text" name="entry.20.single" value="" class="ss-q-short" id="entry_20"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_21">&#29616;&#22312;&#23621;&#20303;&#22320;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_21"></label> 
<input type="text" name="entry.21.single" value="" class="ss-q-short" id="entry_21"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_22">&#26366;&#20219;&#32844;
</label> 
<label class="ss-q-help" for="entry_22"></label> 
<input type="text" name="entry.22.single" value="" class="ss-q-short" id="entry_22"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_23">&#29616;&#22312;&#20219;&#32844;
</label> 
<label class="ss-q-help" for="entry_23"></label> 
<input type="text" name="entry.23.single" value="" class="ss-q-short" id="entry_23"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-section-header"><div class="ss-form-entry"><h2 class="ss-section-title">&#32852;&#31995;&#26041;&#24335;</h2> 
<div class="ss-section-description"></div> 
</div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_26">email
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_26">&#24744;&#24120;&#29992;&#30340;Email</label> 
<input type="text" name="entry.26.single" value="%s" """%user.email()+u""" class="ss-q-short" id="entry_26"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_27">&#30005;&#35805;
<span class="ss-required-asterisk">*</span></label> 
<label class="ss-q-help" for="entry_27"></label> 
<input type="text" name="entry.27.single" value="" class="ss-q-short" id="entry_27"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_29">QQ
</label> 
<label class="ss-q-help" for="entry_29"></label> 
<input type="text" name="entry.29.single" value="" class="ss-q-short" id="entry_29"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_31">MSN
</label> 
<label class="ss-q-help" for="entry_31"></label> 
<input type="text" name="entry.31.single" value="" class="ss-q-short" id="entry_31"></div></div></div> 
<br> <div class="errorbox-good"> 
<div class="ss-item  ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_33">&#36890;&#20449;&#22320;&#22336;*
</label> 
<label class="ss-q-help" for="entry_33"></label> 
<input type="text" name="entry.33.single" value="" class="ss-q-short" id="entry_33"></div></div></div> 
<br> 
<input type="hidden" name="pageNumber" value="0"> 
<input type="hidden" name="backupCache" value=""> 
 
<div class="ss-item ss-navigate"><div class="ss-form-entry"> 
<input type="submit" name="submit" value="&#25552;&#20132;"></div></div></form> 
<script type="text/javascript"> 
      
      (function() {
var error = false;
var divs = document.getElementById('ss-form').
getElementsByTagName('div');
for (var j in divs) {
if (divs[j].className == 'errorbox-bad') {
error = true;
divs[j].lastChild.firstChild.lastChild.focus();
break;
}
}
if (!error) {
for (var i in divs) {
if (divs[i].className == 'ss-form-entry' && divs[i].
firstChild.className == 'ss-q-title') {
divs[i].lastChild.focus();
break;
}
}
}
})();
      </script></div><div class="ss-footer"><div class="ss-attribution"></div></div>  """)
      		else:
			self.response.out.write(u"对不起，请登陆后再进行认证")
		SiteType.Ender(self)
class CerCheck(webapp.RequestHandler):		
	def post(self):
	      user=users.get_current_user()
	      try:
		if user:
			newmember=SiteType.Member()
			newmember.username=user
			newmember.name_chs = self.request.get('entry.0.single')
			newmember.name_eng = self.request.get('entry.1.single')
			strtemp=self.request.get('entry.14.group')
			newmember.sex =  (strtemp==u"男")
			strtemp=self.request.get('entry.2.single')
			datetime1=datetime.datetime.strptime(strtemp,'%Y-%m-%d')
			newmember.birthday=datetime.date(datetime1.year,datetime1.month,datetime1.day)   
			newmember.year_of_grad= int(self.request.get('entry.6.single'))
			newmember.class_in_school = self.request.get('entry.9.single')
			newmember.unversities=[""]
			newmember.unversities[0]=self.request.get('entry.17.single')
			newmember.major=[""]
			newmember.major[0]=self.request.get('entry.15.single')
			newmember.locations=["",""]
			newmember.locations[0]=self.request.get('entry.21.single')
			newmember.locations[1]=self.request.get('entry.20.single')
			newmember.worked=["",""]
			newmember.worked[0]=self.request.get('entry.23.single')
			newmember.worked[1]=self.request.get('entry.22.single')
			newmember.emails=["%s"%user.email()]
			strtemp=self.request.get('entry.26.single')
			if strtemp!=newmember.emails[0]:
				emails[1:1]=strtemp
			newmember.phone=self.request.get('entry.27.single')
			newmember.qq=self.request.get('entry.29.single')
			newmember.msn=self.request.get('entry.31.single')
			newmember.postal=self.request.get('entry.33.single')
			newmember.isadmin=False
			newmember.waitforcer=True
			newmember.put()
		self.redirect(self.request.headers['referer'])
	      except:
		self.redirect(self.request.headers['referer'])
	      	      
def main():
  application = webapp.WSGIApplication([('/cer', CerHandin),('/cercheck',CerCheck),('/cerit',CerIT)],
                                       debug=True)
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()
